#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>
using namespace std;

template<class T>
class thread_pool
{
    const int nums = 5;
public:
    void routine()
    {
        while(true)
        {
            unique_lock<mutex> lck(_mtx);
            while(_tasks.empty())
                _cond.wait(lck);
            T task = _tasks.front();
            _tasks.pop();
            cout << "线程池处理一个任务" <<task << endl;
        }
    }

    void push_task(T& task)
    {
        _tasks.push(task);
        _cond.notify_one();
    }

    thread_pool()
    {
        for(int i = 0; i < nums; i++)
        {
           _threads.push_back([this] { routine(); });
        }
    }

    ~thread_pool()
    {
        for(int i = 0; i < nums; i++)
        {
            _threads[i].join();
        }
    }
private:
    vector<thread> _threads;
    queue<T> _tasks;
    mutex _mtx;
    condition_variable _cond;

};

int main()
{
    unique_ptr<thread_pool<int>> ptr(new thread_pool<int>());
    for(int i = 0; i < 10; i++)
        ptr->push_task(i);
    return 0;
}